package com.lagou.edu.controller;

import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @RequestMapping("/queryAll")
    public ModelAndView queryAll() throws Exception {
        List<Account> accounts = accountService.queryAccountList();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("accounts", accounts);
        modelAndView.addObject("msg", "查询成功");
        modelAndView.setViewName("result");
        return modelAndView;
    }

    @RequestMapping("/transfer")
    public ModelAndView transfer(String fromCardNo,String toCardNo, int money) throws Exception {
        String msg = "转账成功";
        try {
            accountService.transfer(fromCardNo, toCardNo, money);
        } catch (Exception e) {
            msg = "转账失败,原因:" + e.getMessage();
        }
        List<Account> accounts = accountService.queryAccountList();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("accounts", accounts);
        modelAndView.addObject("msg", msg);
        modelAndView.setViewName("result");
        return modelAndView;
    }

}
